package com.cupk.carManageMaster.config;
/**
 * @Software: IntelliJ IDEA
 * @Project: car-manage-master
 * @File: SecurityConfiguration.java
 * @Author: 箬玉
 * @Description:
 * @Date: 2023/5/28 18:40
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

/**
 * @ClassName: SecurityConfiguration
 * @Author: 箬玉
 * @Description:
 * @Date: 2023/5/28 18:40
 */
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
    @Autowired
    AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    LogoutSuccessHandler logoutSuccessHandler;

    //    已弃用
    @Autowired
    SessionInformationExpiredStrategy sessionInformationExpiredStrategy;

    @Autowired
    AccessDeniedHandler accessDeniedHandler;

    @Autowired
    AuthenticationEntryPoint authenticationEntryPoint;

    @Bean
    public BCryptPasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
//        http
//                .authorizeHttpRequests((requests) -> requests
//                                .requestMatchers(HttpMethod.GET,"/*").permitAll()
////                        认证后就可以访问
//                                .anyRequest().authenticated()
////                        拒绝其他请求
////                        .anyRequest().denyAll()
//                )
//                .formLogin((form) -> form
//                        .loginPage("/login")
//                        .permitAll()
//                        .successHandler(authenticationSuccessHandler)
//                        .failureHandler(authenticationFailureHandler)
//                )
//                .logout((logout) -> logout
//                        .permitAll()
//                        .logoutSuccessHandler(logoutSuccessHandler)
//                )
//                .exceptionHandling((except) -> except
//                        .accessDeniedHandler(accessDeniedHandler)
//                        .authenticationEntryPoint(authenticationEntryPoint)
//                )
//                .sessionManagement((session) -> session
//                        .maximumSessions(5)
//                        .expiredSessionStrategy(sessionInformationExpiredStrategy)
//                )
//                .csrf((cs) -> cs
//                        .disable()
//                );
//
        http.csrf((cs) -> cs.disable());
        return http.build();
    }


//    @Bean
//    public UserDetailsService users() {
//        UserDetails user = org.springframework.security.core.userdetails.User.builder()
//                .username("name")
//                .password(getPasswordEncoder().encode("1111"))
//                .roles("user")
//                .build();
//        UserDetails admin = org.springframework.security.core.userdetails.User.builder()
//                .username("admin")
//                .password(getPasswordEncoder().encode("123456"))
//                .roles("vip2")
//                .build();
//        return new InMemoryUserDetailsManager(user, admin);
//    }
}
